package com.sxwl.auth.controller;
import com.sxwl.pojo.User;
import com.sxwl.auth.service.UserService;
import com.sxwl.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

/**
 * @author reading
 * @Description TODO
 * @Date 2020/4/2 20:34
 */
@RestController
@RequestMapping("user/service")
@Api("用户接口")
@EnableAsync
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 校验数据是否可用
     * @param email
     * @return
     */
    @GetMapping("check")
    @ApiOperation(value ="用户邮箱校验",notes = "校验邮箱是否可以用")
    @ApiImplicitParam(name = "email",value = "用户输入邮箱")
    public JsonData checkUserData(@RequestParam("email")  String email) {
        Boolean boo = this.userService.checkData(email);
        if (boo == null||!boo) {
            return JsonData.fail("邮箱不可以");
        }
        return JsonData.success();
    }
    /**
     * 发送验证到邮箱
     * @param email
     * @return
     */
    @PostMapping("code")
    @ApiOperation(value ="发送验证码到邮箱",notes = "根据用户输入邮箱发送验证码")
    @ApiImplicitParam(name = "email",value = "用户输入邮箱")
    @Async
    public JsonData sendVerifyCode(@RequestParam(value = "email",required = true)String email) {
        Boolean boo = this.userService.sendVerifyCode(email);
        if (boo == null || !boo) {
            return JsonData.fail("发送失败");
        }
        return JsonData.success("发送成功");
    }
    /**
     * 注册
     * @param user
     * @param code
     * @return
     */
    @PostMapping("register")
    @ApiOperation(value = "用户注册",notes = "用户登录账号注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name="user",value = "用户注册信息"),
            @ApiImplicitParam(name="code",value = "注册验证码")
    })
    public JsonData register(User user, @RequestParam("code") String code) {
        Boolean bool = this.userService.register(user, code);
        if (bool == null || !bool) {
            return JsonData.fail("注册失败");
        }
        return JsonData.success("注册成功");
    }
    /**
     * 对用户输入邮箱和密码进行校验
     * @param email
     * @param password
     * @return
     */
    @GetMapping("query")
    @ApiOperation(value = "密码和邮箱验证",notes = "对用户输入邮箱和密码进行校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name="email",value = "用户输入邮箱"),
            @ApiImplicitParam(name="password",value = "用户输入密码")
    })
    public ResponseEntity<User> queryUser(
            @RequestParam("email") String email,
            @RequestParam("password") String password
    ) {
        User user = this.userService.queryUser(email, password);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(user);
    }

    /**
     * 在请求头携带用户id
     * @param id  用户id
     */
    @GetMapping("/info")
    @ApiOperation(value = "根据用户id查询用户信息",notes = "根据用户id查询用户信息")
    public JsonData getByidUserInfo(@RequestHeader(value = "uid") String id){
        User byidUserInfo = this.userService.getByidUserInfo(id);
        if(byidUserInfo==null){
            return JsonData.fail("失败");
        }
        return JsonData.success(byidUserInfo,"");
    }
    @PutMapping()
    public JsonData updateUser(User user,@RequestHeader(value = "uid") String id){
             user.setId(Integer.valueOf(id));
        int aBoolean = this.userService.updateUser(user);
        if(aBoolean==1){
            return JsonData.success();
        }
        return JsonData.fail("更新失败");

    }
}
